import axios from "axios";

import { API_URL } from "../config";

const instance = axios.create({
    baseURL: API_URL,
    headers: {
        "Content-Type": "application/json"
        //"Authorization":  getApiToken()//getApiToken() 获取授权码
    },
    timeout: 1000 * 60
});

const _apiTokenKey = "apiToken";
instance.interceptors.request.use(
    config => {
        //    getApiToken(res=>{
        //     config.headers.Authorization=res;
        //   })
        // @ts-ignore
        let token = window.$cookies.get(_apiTokenKey) || "";
        config.headers.Authorization = `Bearer  ${token}`;
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);


instance.interceptors.response.use(
    response => {
        return response.data;
    },
    error => {
        let response = error.response;
        let config = error.config;
        if (response.status === 401) {
            //需要重新获取token
        }else{
            //@ts-ignore
            window.$vm.$toast({
                message: error.message
            });
        }
        return Promise.reject(error);
    }
);

export default instance;
